package com.departmentstore.controller;

import com.departmentstore.entity.Product;
import com.departmentstore.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/products")
@Tag(name = "商品管理", description = "商品的增删改查接口")
public class ProductController {

    private final ProductService productService;

    @Autowired
    public ProductController(ProductService productService) {
        this.productService = productService;
    }

    @PostMapping
    @Operation(summary = "创建商品", description = "创建一个新的商品")
    public ResponseEntity<Product> createProduct(@Valid @RequestBody Product product) {
        return ResponseEntity.ok(productService.createProduct(product));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新商品", description = "根据ID更新商品信息")
    public ResponseEntity<Product> updateProduct(
            @Parameter(description = "商品ID") @PathVariable String id,
            @Valid @RequestBody Product product) {
        product.setProductId(id);
        return ResponseEntity.ok(productService.updateProduct(product));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除商品", description = "根据ID删除商品")
    public ResponseEntity<Void> deleteProduct(
            @Parameter(description = "商品ID") @PathVariable String id) {
        productService.deleteProduct(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/{id}")
    @Operation(summary = "获取商品", description = "根据ID获取商品信息")
    public ResponseEntity<Product> getProduct(
            @Parameter(description = "商品ID") @PathVariable String id) {
        return ResponseEntity.ok(productService.getProductById(id));
    }

    @GetMapping
    @Operation(summary = "获取所有商品", description = "获取所有商品的列表")
    public ResponseEntity<List<Product>> getAllProducts() {
        return ResponseEntity.ok(productService.getAllProducts());
    }

    @GetMapping("/name/{name}")
    @Operation(summary = "根据名称查找商品", description = "根据商品名称查找商品信息")
    public ResponseEntity<List<Product>> getProductsByName(
            @Parameter(description = "商品名称") @PathVariable String name) {
        return ResponseEntity.ok(productService.getProductsByName(name));
    }

    @GetMapping("/category/{category}")
    @Operation(summary = "根据类别查找商品", description = "根据商品类别查找商品信息")
    public ResponseEntity<List<Product>> getProductsByCategory(
            @Parameter(description = "商品类别") @PathVariable String category) {
        return ResponseEntity.ok(productService.getProductsByType(category));
    }

    @GetMapping("/price-range")
    public ResponseEntity<List<Product>> getProductsByPriceRange(
            @RequestParam Double minPrice,
            @RequestParam Double maxPrice) {
        return ResponseEntity.ok(productService.getProductsByPriceRange(minPrice, maxPrice));
    }
} 